<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getHotelList } from '../../api/hotel'
import { showToast } from 'vant'

const router = useRouter()

// 酒店列表
const hotelList = ref<any[]>([])
// 加载状态
const loading = ref(false)
// 是否正在刷新
const refreshing = ref(false)

// 获取酒店列表
const fetchHotelList = async () => {
  try {
    loading.value = true
    const res = await getHotelList()
    hotelList.value = res.data || []
  } catch (error: any) {
    showToast(error.message || '获取酒店列表失败')
  } finally {
    loading.value = false
    refreshing.value = false
  }
}

// 下拉刷新
const onRefresh = () => {
  refreshing.value = true
  fetchHotelList()
}

// 查看酒店详情
const viewHotelDetail = (id: string) => {
  router.push(`/hotel/${id}`)
}

// 页面初始化
onMounted(() => {
  fetchHotelList()
})
</script>

<template>
  <div class="hotels-container">
    <van-nav-bar title="酒店列表" class="hotels-navbar" />
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh" class="pull-container">
      <van-list
        v-model:loading="loading"
        finished
        finished-text="没有更多了"
      >
        <div v-if="hotelList.length === 0 && !loading" class="empty-list">
          <van-empty description="暂无酒店信息" />
        </div>
        <div v-else class="hotel-list">
          <div 
            v-for="hotel in hotelList" 
            :key="hotel.id" 
            class="hotel-card-wrapper"
            @click="viewHotelDetail(hotel.hotelID)"
          >
            <div class="hotel-card">
              <div class="hotel-header">
                <div class="hotel-name">{{ hotel.name }}</div>
                <div class="hotel-rating-badge" v-if="hotel.rating">
                  <van-tag type="primary" round>{{ hotel.rating }}分</van-tag>
                </div>
              </div>
              
              <div class="hotel-content">
                <div class="hotel-info">
                  <div class="hotel-address">
                    <van-icon name="location-o" color="#969799" /> 
                    <span>{{ hotel.address }}</span>
                  </div>
                  <div class="hotel-stars">
                    <van-icon name="hotel-o" color="#969799" />
                    <span>{{ hotel.starRating || 0 }}星级</span>
                    <van-rate 
                      v-model="hotel.starRating" 
                      readonly 
                      size="14" 
                      color="#ffd21e"
                      void-icon="star"
                      void-color="#eee"
                      allow-half
                    />
                  </div>
                </div>
                
                <div class="hotel-divider"></div>
                
                <div class="hotel-footer">
                  <div class="hotel-facilities">
                    <van-tag plain type="primary" class="facility-tag">点击查看</van-tag>
                  </div>
                  <div class="hotel-more">
                    <van-icon name="arrow" />
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<style scoped>
.hotels-container {
  min-height: 100%;
  background-color: #f7f8fa;
  display: flex;
  flex-direction: column;
}

.hotels-navbar {
  background-color: #ffffff;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.05);
}

.pull-container {
  flex: 1;
  overflow: auto;
}

.hotel-list {
  padding: 12px;
}

.empty-list {
  padding: 80px 0;
}

.hotel-card-wrapper {
  margin-bottom: 12px;
}

.hotel-card {
  background-color: #ffffff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  transition: transform 0.3s, box-shadow 0.3s;
}

.hotel-card:active {
  transform: scale(0.98);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.hotel-header {
  padding: 14px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #f8f8f8;
  border-bottom: 1px solid #f2f2f2;
}

.hotel-name {
  font-size: 16px;
  font-weight: bold;
  color: #323233;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hotel-content {
  padding: 0;
}

.hotel-info {
  padding: 12px 16px;
}

.hotel-address {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  color: #646566;
  font-size: 13px;
}

.hotel-address span {
  margin-left: 4px;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hotel-stars {
  display: flex;
  align-items: center;
  color: #646566;
  font-size: 13px;
}

.hotel-stars span {
  margin: 0 6px;
}

.hotel-divider {
  height: 1px;
  background-color: #f5f5f5;
  margin: 0 16px;
}

.hotel-footer {
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.hotel-facilities {
  display: flex;
  flex-wrap: wrap;
  flex: 1;
}

.facility-tag {
  margin-right: 6px;
  margin-bottom: 4px;
}

.hotel-more {
  color: #969799;
}
</style> 